/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solucionessgh.TipoContacto;

/**
 *
 * @author roderick
 */


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;

/**
 *
 * @author roderick
 */
public class TipoContactoModel extends DataBase{
    //SELECT tcncod, tcndesc, tcnabr FROM tipocontacto;
    private int id;
    private String tcndesc;
    private String tcnabr;

    public TipoContactoModel(){

    }

    private boolean validar(){
        /*if(this.tcndesc.length()>20){
            JOptionPane.showMessageDialog(null,
            "Descripcion menor a 20 digito",
            "Nivel Usuario",
            JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if(this.tcnabr.length()!=3){
            JOptionPane.showMessageDialog(null,
            "Codigo debe tener tres digito",
            "Nivel Usuario",
            JOptionPane.ERROR_MESSAGE);
            return false;
        }*/
        return true;

    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("INSERT INTO tipocontacto(tcndesc, tcnabr) VALUES (?, ?);");
                stmt.setString(1,this.tcndesc);
                stmt.setString(2,this.tcnabr);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("UPDATE tipocontacto SET tcndesc=?, tcnabr=? where tcncod = ? ;");
                stmt.setString(1,this.tcndesc);
                stmt.setString(2,this.tcnabr);
                stmt.setInt(3, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public boolean delete(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from tipocontacto where tcncod=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT tcncod, tcndesc, tcnabr FROM tipocontacto order by tcndesc ");
    }


    public ResultSet finder(String var){
        return this.find("SELECT tcncod, tcndesc, tcnabr FROM tipocontacto WHERE tcndesc LIKE '"+var+"%' order by tcndesc;");
    }

    public ResultSet finder(int var){
        return this.find("SELECT tcncod, tcndesc, tcnabr FROM tipocontacto WHERE tcncod = "+var+";");
    }



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String gettcnabr() {
        return tcnabr;
    }

    public void settcnabr(String tcnabr) {
        this.tcnabr = tcnabr;
    }

    public String gettcndesc() {
        return tcndesc;
    }

    public void settcndesc(String tcndesc) {
        this.tcndesc = tcndesc;
    }
}



